package myapps.ttclient.connection;

import java.io.IOException;

import myapps.ttclient.activity.connection.ConnectionEditActivity;
import myapps.ttclient.activity.connection.ConnectionEditActivity_Wifi;
import myapps.ttclient.app.TTClientApplication;
import myapps.ttclient.baseclass_and_interface.TTClientConnection;
import myapps.ttclient.protocol.PhoneClientConnectionTcp;



import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class Connection_Wifi extends Connection
{
	private String host;
	private int port;
	
	public Connection_Wifi()
	{
		super();
		
		this.host = "192.168.1.1";
		this.port = PhoneClientConnectionTcp.DEFAULT_PORT;
	}
	
	public static Connection_Wifi load(SharedPreferences preferences, int position)
	{
		Connection_Wifi connection = new Connection_Wifi();
		
		connection.name = preferences.getString("connection_wifi_" + position + "_name", "");
		connection.host = preferences.getString("connection_wifi_" + position + "_host", "");
		connection.port = preferences.getInt("connection_wifi_" + position + "_port", 0);
		
		return connection;
	}
	
	@Override
	public void save(Editor editor, int position)
	{
		editor.putString("connection_wifi_" + position + "_name", this.name);
		editor.putString("connection_wifi_" + position + "_host", this.host);
		editor.putInt("connection_wifi_" + position + "_port", this.port);
	}
	
	@Override
	public void edit(Context context)
	{
		Intent intent = new Intent(context, ConnectionEditActivity_Wifi.class);
		
		intent.putExtra("name", this.name);
		intent.putExtra("host", this.host);
		intent.putExtra("port", this.port);
		
		intent.putExtra("type", "edit");
		
		ConnectionEditActivity.connectionParam = this;
		
		context.startActivity(intent);
	}
	
	@Override
	public TTClientConnection connect(TTClientApplication application) throws IOException
	{
		return PhoneClientConnectionTcp.create(this.host, this.port);
	}
	
	public String getHost()
	{
		return host;
	}
	
	public void setHost(String host)
	{
		this.host = host;
	}
	
	public int getPort()
	{
		return port;
	}
	
	public void setPort(int port)
	{
		this.port = port;
	}
}
